package com.partners.controller.test;

/**
 * 主要线程
 * @author liangyongle
 * @date 2019-02-12
 */
public class Stage extends Thread {

    public void run() {
        System.out.println("欢迎观看");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        Threads army = new Threads();
        Threads task = new Threads();


        //使用Runnable创建线程
        Thread armyOf = new Thread(army, "隋军");
        Thread taskOf = new Thread(task, "农民起义军发起进攻");

        armyOf.start();
        taskOf.start();
        //舞台线程休眠，
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

//        try {
//            armyOf.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        army.KeepRunning = false;
//        task.KeepRunning = false;

        //半路杀出程咬金
        System.out.println("双方交战正酣，半路杀出程咬金");

        Thread MrCheng = new KeyPersonThread();
        MrCheng.setName("程咬金");
        System.out.println("程咬金保家卫国，安居乐业");

        Thread keyperson = new Thread(MrCheng, "程咬金");
        //军队停止作战
        army.KeepRunning = false;
        task.KeepRunning = false;

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //主角进场
        MrCheng.start();
        //所有线程都要等待join执行之后
        try {
            MrCheng.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //MrCheng.KeepRunning = false;
        System.out.println("战争结束");


    }

    public static void main(String[] args) {
        new Stage().start();
    }
}
